const hexList: string[] = [];
for (let i = 0; i <= 15; i++) {
    hexList[i] = i.toString(16);
}

export function isNull(value: any): boolean {
    return value === null || value === undefined;
}

export function isFunction(value: any): value is Function {
    if(isNull(value)){
        return false;
    }
    return typeof value === 'function';
}

export function isPromise<T = any>(value: any): value is Promise<T> {
    return value && typeof value.then === 'function' && typeof value.catch === 'function';
}

export function buildUUID(): string {
    let uuid = '';
    for (let i = 1; i <= 36; i++) {
        if (i === 9 || i === 14 || i === 19 || i === 24) {
            uuid += '-';
        } else if (i === 15) {
            uuid += 4;
        } else if (i === 20) {
            uuid += hexList[(Math.random() * 4) | 8];
        } else {
            uuid += hexList[(Math.random() * 16) | 0];
        }
    }
    return uuid.replace(/-/g, '');
}



let unique = 0;
export function buildShortUUID(prefix = ''): string {
    const time = Date.now();
    const random = Math.floor(Math.random() * 1000000000);
    unique++;
    return prefix + '_' + random + unique + String(time);
}


export function getDataType(data: any, assertionsType: String): String | Boolean {
    if (data) {
        let type = data.nodeName ? 'HTML' : Object.prototype.toString.apply(data);
        let actualType = "";
        switch (type) {
            case '[object Number]':
                actualType = 'num';
                break;
            case '[object Array]':
                actualType = 'arr';
                break;
            case '[object String]':
                actualType = 'str';
                break;
            case '[object Function]':
                actualType = 'fun';
                break;
            case '[object Object]':
                actualType = 'obj';
                break;
            case '[object RegExp]':
                actualType = 'reg';
                break;
            default:
                if (type && type.toUpperCase().indexOf('HTML') != -1) {
                    actualType = 'html';
                } else {
                    actualType = "";
                }
                break;
        }
        return assertionsType ? actualType == assertionsType ? true : false : actualType;
    }
    return false;
}
